

clear
set more 1
set type double

capture log using "log/Table5_erpt_annual.log", replace

*******************************************************************************

use  ../../rawdata/X_2019, clear
	append using ../../rawdata/X_2018
	append using ../../rawdata/X_2017
	append using ../../rawdata/X_2016
	append using ../../rawdata/X_2015
	append using ../../rawdata/X_2014
	append using ../../rawdata/X_2013
	append using ../../rawdata/X_2012
gen hs4=substr(cncode,1,4)
	destring hs4, replace
	gen hs6=substr(cncode,1,6)
	
merge m:1 hs6 using ../data/Rauch_hs6_concordance, keep(match master) 
	gen dif=con=="n" if _merge==3	
	keep if dif==1
	
	gcollapse (sum) value, by(vat hs4)
	egen tot=sum(value), by(vat)
	gen sh=value/tot
	bys vat (sh): egen maxsh=max(sh)
	gen main=sh==maxsh
	keep if main==1
	*drop if sh<0.8
	drop if sh<0.6
	keep vat hs4
save temp/main_annual, replace	


*1. prepare exchange rate data
**************************************************************
use ../rawdata/xr_monthly_2020, clear
	keep year cty_name cty_code xr_y
	duplicates drop
	drop if missing(xr_y)
	egen cty_num=group(cty_code)
	tsset cty_num year

*xr_y is euro per destination
*redefine exchange rate so an increase is a depreciation of the destination currency
*gen destination per euro
	gen xE=1/xr_y
	gen dxE=ln(xE/l.xE)
*drop outliers: Venezuela, Sudan, Uzbekastan 
	drop if cty_code=="VE"|cty_code=="SD"|cty_code=="UZ"
	drop if abs(dxE)>1
	keep cty_code year xE dxE
save temp/er_annual_xE,replace


use ../rawdata/xr_monthly_2020, clear
	keep year cty_name cty_code xr_y
	duplicates drop
	drop if missing(xr_y)
	egen cty_num=group(cty_code)

preserve
	keep if cty_code=="US"
	gen euro_dollar=xr_y 
	gen xED=1/xr_y
	keep year euro_dollar xED
	duplicates drop
	save temp/euro_dollar, replace
restore
	merge m:1 year using temp/euro_dollar, keep(match) nogen
	tsset cty_num year	
	gen xD=(1/xr_y)*euro_dollar
	gen dxD=ln(xD/l.xD)
	gen dxED=ln(xED/l.xED)
	*drop outliers: Venezuela, Sudan, Uzbekastan 
	drop if cty_code=="VE"|cty_code=="SD"|cty_code=="UZ"
	
	merge 1:1 cty_code year using temp/er_annual_xE, keep(match) nogen
	keep cty_code cty_name year xE xD euro_dollar dxD dxE dxED
	
	save temp/er_annual_xD,replace	

******************************************
*2. construct comprehensive ms measures
*averaged over 2012 to 2019
**************************************
use ../data/X_hs4_2012_2019, clear
	destring hs4, replace
save temp/X_hs4_2012_2019, replace	

use  ../../rawdata/X_2019, clear
	append using ../../rawdata/X_2018
	append using ../../rawdata/X_2017
	append using ../../rawdata/X_2016
	append using ../../rawdata/X_2015
	append using ../../rawdata/X_2014
	append using ../../rawdata/X_2013
	append using ../../rawdata/X_2012
	rename land cty_code
	drop if cty_code=="EU"	
	drop if flow=="XI"
	gen hs4=substr(cncode,1,4)
	destring hs4, replace
	collapse (sum) value, by(vat hs4 cty_code year)	
	merge m:1 cty_code hs4 year using temp/X_hs4_2012_2019, keep(match) nogen
	gen ms_y=value/totxvalue
	replace ms_y=1 if ms_y>1
	collapse (mean) ms_y, by(vat hs4 cty_code)
save temp/ms_all, replace


******************************************
*3. construct unit values in export data
use  ../../rawdata/X_2020, clear	
	append using ../../rawdata/X_2019
	append using ../../rawdata/X_2018
	append using ../../rawdata/X_2017
	append using ../../rawdata/X_2016
	append using ../../rawdata/X_2015
	append using ../../rawdata/X_2014
	append using ../../rawdata/X_2013
	append using ../../rawdata/X_2012

	drop if flow=="XI"
	replace weight=. if weight==0
	replace units=. if units==0
	drop if missing(units) & missing(weight)
	collapse (sum) value weight units, by(vat year cncode land)
	rename land cty_code
		
	gen uv1=value/weight
	gen uv2=value/units 
	
	gen q1=weight
	gen q2=units 
	
	egen fpd = group(vat cncode cty_code)
	tsset fpd year
		
	gen dq1=ln(q1/l.q1)
	gen dq2=ln(q2/l.q2)
	gen dq=dq1
	replace dq=dq2 if missing(dq)
	gen qratio=(q1/l.q1)
	replace qratio=q2/l.q2 if missing(qratio)
	
	gen duv1=uv1/l.uv1
	gen duv2=uv2/l.uv2
	
	gen lagvalue=l.value
	gen duv=ln(uv1/l.uv1)
	replace duv=ln(uv2/l.uv2) if missing(duv)
	gen pratio=uv1/l.uv1
	replace pratio=uv2/l.uv2 if missing(pratio)
		
	drop if missing(duv)
	drop if missing(dq)
		
	drop if missing(pratio)|missing(qratio)
	
	gen clean=1 if (pratio>3| pratio<1/3)
	drop if clean==1
	
	
** oecd indicator **
***********************
	merge m:1 cty_code using ../rawdata/cty_name, keep(match master) nogen
	gen oecd = 1 if inlist(cty_name, "AUSTRALIA", "AUSTRIA","LUXEMBOURG","CANADA", "DENMARK", "FINLAND")
	replace oecd= 1 if inlist(cty_name, "FRANCE", "GERMANY", "GREECE","ICELAND","IRELAND")
	replace oecd=1 if inlist(cty_name,"ITALY", "JAPAN","NETHERLANDS","NEW ZEALAND", "NORWAY")
	replace oecd=1 if inlist(cty_name,"PORTUGAL","SPAIN","SWEDEN", "SWITZERLAND", "TURKEY")
	replace oecd=1 if inlist(cty_name,"UNITED KINGDOM", "UNITED STATES","MEXICO", "KOREA, REPUBLIC OF")
	replace oecd=1 if inlist(cty_name, "CZECH REPUBLIC","HUNGARY", "POLAND",  "SLOVAKIA", "CHILE","ESTONIA", "SLOVENIA", "ISRAEL")
	
	*only keep countries that are advanced according to IMF and/or high income according to World Bank
	replace oecd=0 if inlist(cty_name, "CHILE", "HUNGARY", "MEXICO", "POLAND","TURKEY")
	replace oecd=0 if oecd==.	

*merge in data
***********************
	merge m:1 cty_code year using temp/er_annual_xD, keep(match) nogen
	merge m:1 vat using ../data/memp, keep(match master) nogen	
	merge m:1 vat using ../data/mphi_xeu_currency, keep(match) nogen
	merge m:1 vat cty_code using ../data/currency_vat, keep(match) nogen 
	merge m:1 cty_code using ../data/usd_peg, keep(match master) nogen
		
gen hs4=substr(cncode,1,4)
	destring hs4, replace
	merge m:1 vat hs4 cty_code using temp/ms_all, keep(match master) nogen	

gen hs6=substr(cncode,1,6)
	merge m:1 hs6 using ../data/Rauch_hs6_concordance, keep(match master) 
	gen dif=con=="n" if _merge==3
	drop _merge

merge m:1 vat hs4 using temp/main_annual, keep(match master)
	gen main=_merge==3
	drop _merge
	
*merge manuf indicators
	merge m:1 vat using ../data/manuf_firm, keep(match) nogen
	keep if manuf==1

*************************************
*construct variables
***********************************
	gen lnmemp=ln(memp)
	drop if missing(lnmemp)
		
*dxr interactions
	gen dxE_phi=dxE*mphi_xeu
	gen dxE_phi_other=dxE*mphi_xeu_other
	gen dxE_phi_euro=dxE*mphi_xeu_euro
	gen dxE_phi_missing=dxE*mphi_xeu_missing
	gen dxE_ms_y=dxE*ms_y
	gen dxE_lnmemp=dxE*lnmemp
		
*dxrD interactions
	gen dxD_phi=dxD*mphi_xeu
	gen dxD_phi_other=dxD*mphi_xeu_other
	gen dxD_phi_euro=dxD*mphi_xeu_euro
	gen dxD_phi_missing=dxD*mphi_xeu_missing
	gen dxD_ms_y=dxD*ms_y
	gen dxD_lnmemp=dxD*lnmemp
	
*currency share interactions
	gen share_other=1-share_euro
	gen dxE_share_other=dxE*share_other
	gen dxD_share_usd=dxD*share_usd
	gen dxD_share_other=dxD*share_other
	gen dxED_share_other=dxED*share_other
	
*convert duv from euro prices to destination prices
	gen duv_k=duv + dxE
	

*fe
	egen cp=group(hs4 cty_code)
	egen cy=group(year cty_code)
	egen cpy=group(cty_code hs4 year)

save ../data/erpt_annual_estdata, replace


**************************************
*Table 5
*all dropping share_vehicle_other>10%
***************************************
*i. without ms
****************************************************************
use ../data/erpt_annual_estdata, clear
	merge m:1 vat cty_code using ../data/currency_vehicle, keep(match master) nogen	
	drop if vehicle_other==1

*col1 - all countries
	eststo: reghdfe duv_k dxE dxE_phi dxE_lnmemp dxE_share_other dxD_phi dxD_lnmemp dxD_share_usd lnmemp mphi_xeu share_other share_usd , cluster(cy) absorb(cp year)

*col2 - all countries - only flexible
	eststo: reghdfe duv_k dxE dxE_phi dxE_lnmemp dxD_phi dxD_lnmemp lnmemp mphi_xeu , cluster(cy) absorb(cp year)

*col3 - all countries - only sticky
	eststo: reghdfe duv_k dxE dxE_share_other dxD_share_usd share_other share_usd , cluster(cy) absorb(cp year)

*col4 - all countries with cpy fe	
	eststo: reghdfe duv_k dxE_phi dxE_lnmemp dxE_share_other dxD_phi dxD_lnmemp dxD_share_usd lnmemp mphi_xeu share_other share_usd , cluster(cy) absorb(cpy)	

*col5 only pegs
	eststo: reghdfe duv_k dxE dxE_phi dxE_lnmemp dxE_share_other mphi_xeu lnmemp share_other if usd_peg==1, cluster(cy) absorb(cp year)

*col6 - non pegs
	eststo: reghdfe duv_k dxE dxE_phi dxE_lnmemp dxE_share_other dxD_phi dxD_lnmemp dxD_share_usd lnmemp mphi_xeu share_other share_usd if usd_peg==0, cluster(cy) absorb(cp year)



esttab using "../results/Table5_erpt_annual.csv", order(dxE dxE_phi dxD_phi dxE_lnmemp dxD_lnmemp dxE_share_other dxD_share_usd lnmemp mphi_xeu share_other share_usd) se b(3) se(3) r2 ar2 star(* 0.10 ** 0.05 *** 0.01) compress replace
eststo clear	




********************************
*ROBUSTNESS
*Table A5
*********************************************
*size dummies
gen large500=memp>=500
gen dxE_large500=dxE*large500
gen dxD_large500=dxD*large500	


*col1 - all countries with firm fe
	eststo: reghdfe duv_k dxE dxE_phi dxE_lnmemp dxE_share_other dxD dxD_phi dxD_lnmemp dxD_share_usd ms_y lnmemp mphi_xeu share_other share_usd , cluster(cy) absorb(cp year vat)

*col2 - employment bin
	eststo: reghdfe duv_k dxE dxE_phi dxE_large500 dxE_share_other dxD dxD_phi dxD_large500 dxD_share_usd large500 mphi_xeu share_other share_usd , cluster(cy) absorb(cp year)


*different subsamples
*col3 - dif
	eststo: reghdfe duv_k dxE dxE_phi dxE_lnmemp dxE_share_other dxD dxD_phi dxD_lnmemp dxD_share_usd mphi_xeu share_other share_usd if dif==1, cluster(cy) absorb(cp year)


*col4 - nondif
	eststo: reghdfe duv_k dxE dxE_phi dxE_lnmemp dxE_share_other dxD dxD_phi dxD_lnmemp dxD_share_usd lnmemp mphi_xeu share_other share_usd if dif==0, cluster(cy) absorb(cp year)

*col5 - oecd
	eststo: reghdfe duv_k dxE dxE_phi dxE_lnmemp dxE_share_other dxD dxD_phi dxD_lnmemp dxD_share_usd lnmemp mphi_xeu share_other share_usd if oecd==1, cluster(cy) absorb(cp year)

*col6 - US only
	eststo: reghdfe duv_k dxE dxE_phi dxE_lnmemp dxE_share_other lnmemp mphi_xeu share_other if cty_code=="US", cluster(cy) absorb(cp year)

*col7 - non pegs and no dollar interactions
	eststo: reghdfe duv_k dxE dxE_phi dxE_lnmemp dxE_share_other lnmemp mphi_xeu share_other if usd_peg==0, cluster(cy) absorb(cp year)

*col8 - main product
	eststo: reghdfe duv_k dxE dxE_phi dxE_lnmemp dxE_share_other dxD dxD_phi dxD_lnmemp dxD_share_usd lnmemp mphi_xeu share_other share_usd if main==1, cluster(cy) absorb(cp year)

*col9 - drop 2020
eststo: reghdfe duv_k dxE dxE_phi dxE_lnmemp dxE_share_other dxD dxD_phi dxD_lnmemp dxD_share_usd lnmemp mphi_xeu share_other share_usd if year<2020, cluster(cy) absorb(cp year)


use ../data/erpt_annual_estdata, clear

*col 10 - don't drop vehicle other
eststo: reghdfe duv_k dxE dxE_phi dxE_lnmemp dxE_share_other dxD dxD_phi dxD_lnmemp dxD_share_usd lnmemp mphi_xeu share_other share_usd , cluster(cy) absorb(cp year)



esttab using "../results/AppTableB4_erpt_robust.csv", order(dxE dxE_phi dxD_phi dxE_lnmemp dxD_lnmemp dxD_ms_y dxE_ms_y dxE_high dxD_high dxE_share_other dxD_share_usd lnmemp large500 mphi_xeu share_other share_usd) se b(3) se(3) r2 ar2 star(* 0.10 ** 0.05 *** 0.01) compress replace
eststo clear	

